package com.com.day05;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class BallJPanel extends JPanel implements MouseListener, MouseMotionListener {
    //坐标x，y半径类型（敌机或子弹）运动方向（上或下）图片
    //玩家的飞机
    public boolean run=true;
public  boolean stop=false;
    public int l=600;
    //英雄图片
   public  Ball hero;
   //星星坐标
    public int[] xx;
    public int[] yy;
    //星星大小
    public Font[] fs;
    //无限的敌机
    public List<Ball> foes;
    //敌机子弹
    public List<Ball>fires1;


    //无限的子弹
    public List<Ball> fires;
    public Image img_beijing=new ImageIcon("beijing.png").getImage();
    //是否发射子弹
    public boolean isFire=false;
    public  boolean iFire=false;
public int scores=0;
    public BallJPanel(){
        hero=new Ball();
        //英雄坐标
        hero.x=420;
        hero.y=900;
        hero.type=Ball.HERO;hero.r=20;
        //星星坐标
        xx=new int[300];
        yy=new int[300];
        for (int i=0;i<xx.length;i++){
            //随机生成星星坐标
            xx[i]=new Random().nextInt(800);
            yy[i]=new Random().nextInt(1000);
        }
      fs=new Font[5];
        for (int i=0;i<fs.length;i++){
            fs[i]=new Font("宋体",Font.BOLD,12+i*2);
        }
        //无限敌机
        foes=new ArrayList<Ball>();
        //无限子弹
        fires=new ArrayList<Ball>();
        fires1=new ArrayList<Ball>();
        //添加监听
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        job_Music JP = new job_Music();
        JP.m();

        //设置背景颜色
        this.setBackground(Color.black);
        g.drawImage(img_beijing,0,0,1024,800,null);
        //画英雄
        hero.draw(g);

        //画星星
        for (int i=0;i<xx.length;i++){
            //设置星星颜色
            g.setColor(new Color(new Random().nextInt(0xffffff)));
            //设置星星大小
            g.setFont(fs[new Random().nextInt(5)]);
            //画星星
            g.drawString("*",xx[i],yy[i]);


        }
        //绘制所有敌机
        for (int i=0;i<foes.size();i++){
            Ball ball=foes.get(i);
            ball.draw(g);
        }
        //绘制所有子弹
        for (int i=0;i<fires.size();i++){
            Ball ball=fires.get(i);
            ball.draw(g);
        }
        for (int i=0;i<fires1.size();i++){
            Ball ball=fires1.get(i);
            ball.draw(g);
        }

        //积分榜
        g.setColor(Color.white);

        g.setFont(new Font("宋体",Font.BOLD,20));
        g.drawString("分数:"+scores,600,40);
        //飞机血量
        g.setColor(Color.white);
        g.drawRect(750,50,30,600);
        g.setColor(Color.red);
        g.fillRect(750,50,29,l);
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }
//鼠标按压触发
    @Override
    public void mousePressed(MouseEvent e) {
       isFire=true;
    }
//鼠标释放触发
    @Override
    public void mouseReleased(MouseEvent e) {
       isFire=false;
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
//鼠标拖动
    @Override
    public void mouseDragged(MouseEvent e) {
          hero.x=e.getX()-20;
          hero.y=e.getY()-20;
          repaint();
    }
    //鼠标移动
    @Override
    public void mouseMoved(MouseEvent e) {
        hero.x=e.getX()-20;
        hero.y=e.getY()-20;
        repaint();
    }
    //通过线程控制 实现画板内容的改变
    public void startRun(){
        Thread thread = new Thread(){
            @Override
            public void run() {
                //如果计数器达到最大数，让其归零
                int count=0;
                while (run){
                    count++;
                    if (count>=Integer.MAX_VALUE){
                        count=0;
                    }
                    //try，catch代码包围
                    try {
                        //线程睡眠
                        Thread.sleep(40);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //不停产生敌机
                    if (count%30==0){
                        Ball ball = new Ball();
                        Ball ball1 = new Ball();
                        ball.x=new Random().nextInt(800);
                        ball.y=-40;
                        ball.r=20;
                        ball1.x=ball.x+15;
                        ball1.y=ball.y+60;
                        ball1.r=10;
                        ball1.type=Ball.FIRE1;
                        ball1.ori=Ball.DOWN;
                        ball.type=Ball.FOE;
                        ball.ori=Ball.DOWN;
                        //把敌机放入要绘制的敌机集合中
                        foes.add(ball);
                        fires1.add(ball1);
                    }
                    //让敌机向下运动
                    for (int i=0;i<foes.size();i++){
                       for (int j=0;j<fires1.size();j++) {
                           Ball ball=null;
                           if(i<foes.size()){
                             ball = foes.get(i);}
                          ball.move();
                          Ball ball1=fires1.get(j);
                          ball1.move();
                            if (ball.y >800) {
                              foes.remove(i);
                               }
                              if (ball1.y>800){
                                  fires1.remove(j);
                              }
                         }
                    }
                    //不停的绘制子弹
                    if (isFire&&count%6==0){
                        Ball ball=new Ball();
                        ball.x=hero.x+5;
                        ball.y=hero.y-20;
                        ball.r=20;
                        ball.type=Ball.FIRE;
                        ball.ori=Ball.UP;
                        //把子弹放入子弹集合

                        fires.add(ball);
                    }

                    //子弹向上移动
                    for (int i=0;i<fires.size();i++){
                        Ball ball = fires.get(i);
                        ball.move();
                        if (ball.y<=0){
                            fires.remove(i);
                        }
                    }



                    //判断敌机和子弹是否相遇
                    for (int i=0;i<fires.size();i++){
                        for (int j=0;j<foes.size();j++){
                            if (i<fires.size()&&j<foes.size()&& fires.get(i).isTouch(foes.get(j))){
                                scores+=20;
                                fires.remove(i);
                                foes.remove(j);
                            }

                        }
                    }
                     //敌机子弹与英雄
                    for (int i=0;i<fires1.size();i++){
                        if (i<fires1.size()&& hero.isTouch(fires1.get(i))){


                            fires1.remove(i);

                            isFire=false;
                            l-=100;
                            if (l==0){
                                hero.r=0;
                                fires.remove(i);
                                run=false;
                                GameOver go=new GameOver();
                            }
                        }


                    }

                    for (int i=0;i<foes.size();i++){
                        if (i<foes.size()&& hero.isTouch(foes.get(i))){

                        GameFrame.panel.setVisible(true);
                            GamePanel.ballJFrame.setVisible(false);

                            foes.remove(i);
                            hero.r=0;
                            isFire=false;

                        }


                    }
                    repaint();
                }//while
            }//run
        };//Thread
        thread.start();
    }
}
